package com.boc.langxiang.net;


import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * CreateTime: 2016-04-11上午9:48
 * Author: wjkjinke00@126.com
 * Description:
 */
public class NetClient {

    private static final int TIME_OUT = 10 * 1000;
    private static final String TEST = "http://xjw.hzboc.com/langxiang/api/index.php/";
    private static final String LOCAL_TEST = "http://192.168.1.4:8089/langxiang/api/index.php/";
    private static final String RELEASE = "http://xjw.hzboc.com/langxiang/api/index.php/";

    public static final String BASE_URL = TEST;

    public static final String UPLOAD_IMAGE = "upload/photo";

    private Retrofit mRetrofit;

    private NetClient() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(getHttpClient())
                .addConverterFactory(MyGsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    private static class SingletonHolder {
        private static final NetClient INSTANCE = new NetClient();
    }

    public static NetClient getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public Retrofit net() {
        return mRetrofit;
    }

    private OkHttpClient getHttpClient() {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .readTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();

                        long t1 = System.nanoTime();
                        final Logger logger = Logger.getLogger("retrofit");

                        StringBuilder sb = new StringBuilder();
                        sb.append(request.url());
                        sb.append("?");

                        FormBody formBody = (FormBody) request.body();
                        int size = formBody.size();
                        for (int i = 0; i < size; i++) {
                            logger.info(formBody.encodedName(i) + "=" + formBody.encodedValue(i));
                            sb.append(formBody.encodedName(i));
                            sb.append("=");
                            sb.append(formBody.encodedValue(i));
                            if (i != size - 1) {
                                sb.append("&");
                            }
                        }
                        logger.info(sb.toString());

                        Response response = chain.proceed(request);

                        long t2 = System.nanoTime();
                        logger.info(String.format("Received response for %s in %.1fms%n%s",
                                response.request().url(), (t2 - t1) / 1e6d, response.headers()));

                        //会导致 IllegalStateException close
//                        String body = response.body().string();
//                        KLog.json(body);
                        return response;
                    }
                })
                .build();
        return client;
    }

}
